<?php
// +----------------------------------------------------------------------
// | YFCMF [ WE CAN DO IT MORE SIMPLE ]
// +----------------------------------------------------------------------
// | Copyright (c) 2015-2016 http://www.rainfer.cn All rights reserved.
// +----------------------------------------------------------------------
// | Author: rainfer <81818832@qq.com>
// +----------------------------------------------------------------------
namespace Home\Controller;
use Home\Controller\HomebaseController;
use Com\WechatJsapi;
class NewsController extends HomebaseController {
	
	//微信的一些信息   uid是数据库表里依据
  	private $dbName ='wechatjs';
  	private $uid = 0;
	
	//微信脚本权限调用,用于纯静态axaj返回   这里这$appid,$appSecret,$dbName,$uid  下面去数据库取
	Public  function getWechatJsaip(){
		$sys = S('sysweixin');
        if(!$sys){
			$sys=M('options')->where(array('option_name'=>'weixin_options'))->getField("option_value");   //取微信信息
			$sys =json_decode($sys,true);
            S('sysweixin', $sys, 86400);   //缓存下
        }
        //定义对象
        $wechat = new WechatJsapi($sys["wesys_appid"],$sys["wesys_appsecret"],$this->dbName,$this->uid);
		$wechatjs = $wechat -> getSignPackage();
		
		return $wechatjs;	     //返回微信js相关
	}

    //文章内页
    public function index() {
            
             //更新点击数
            //自行根据网站需要考虑，是否需要判断,这里不做检查
	    // $can_do=check_user_action('news'.I('id'),0,false,60);
        
            M('news')->save(array("n_id"=>I('id'),"news_hits"=>array("exp","news_hits+1")));
            $news['news_hits']+=1;

            $wechatjs = $this -> getWechatJsaip();

            $id=intval($_GET['id']);//文章id
            
            $news = S('site_lists_'.$id);
            if(!$news){        
                $join = "".C('DB_PREFIX').'admin as b on a.news_auto =b.admin_id';
                $news=M('news')->alias("a")->join($join)->where(array('n_id'=>I('id'),'news_open'=>1,'news_back'=>0))->find();
				
                if(empty($news))die ( '页面错误,请稍后打开');
				
				$news['news_extra'] = wp_explode($news['news_extra'],"\r\n");
                S('site_lists_'.$id, $news, 86400);  
            }
            $fields = S('xianmu'.$news['n_id']);
            if(!$fields){
                $dao = M ('forms_attribute');
                $save ['forms_id'] = $news['n_id'];
                $fields = $dao -> where ( $save ) -> order ( 'sort asc, id asc' )-> select();
                foreach ($fields as $key => $value) {
                        if(!empty($value['extra'])){
                                $fields[$key]['ratitle'] = explode('|',$value['extra']); 
                        }	
                };
                 S('xianmu'.$news['n_id'], $fields, 86400);  
            }
            $news['news_img'] = 'http://'.$_SERVER['HTTP_HOST'].$news['news_img'];
            $url = U('index',array('id' => I('id')),'html',true);
            $this->assign ( 'url', $url);
            $this ->assign('wechatjs',$wechatjs);      //微信脚本
            $this->assign ( 'fields', $fields );
            $this->assign ( 'forms_id', $news['n_id']);
            $this->assign("news",$news);
            
            $html_info=$this-> display('news');
            
            
    }
    
    public function dolike(){
	    $this->check_login();
    	$id=intval($_GET['id']);
    	$news_model=M("news");
    	$can_like=check_user_action('news'.$id,1);
    	if($can_like){
    		$news_model->save(array("n_id"=>$id,"news_like"=>array("exp","news_like+1")));
    		$this->success("赞好啦！",1,1);
    	}else{
    		$this->error("您已赞过啦！",0,0);
    	}
    }
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	function dofavorite(){
        $this->check_login();
		$key=I('key');
		if($key){
			$id=I('id');
			if($key==encrypt_password('news-'.$id,'news')){
				$uid=session('hid');
				$favorites_model=M("favorites");
				$find_favorite=$favorites_model->where(array('t_name'=>'news','t_id'=>$id,'uid'=>$uid))->find();
				if($find_favorite){
					$this->error("亲，您已收藏过啦！",0,0);
				}else {
                    $data=array(
                        'uid'=>$uid,
                        't_name'=>'news',
                        't_id'=>$id,
                        'createtime'=>time(),
                    );
					$result=$favorites_model->add($data);
					if($result){
						$this->success("收藏成功！",1,1);
					}else {
						$this->error("收藏失败！",0,0);
					}
				}
			}else{
				$this->error("非法操作，无合法密钥！",0,0);
			}
		}else{
			$this->error("非法操作，无密钥！",0,0);
		}
	}
}
